﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Biz;

namespace control.Dal
{
    class DalKhachHang
    {
        internal static control.Biz.BizKhachHang DangNhap(control.Biz.BizKhachHang bizKhachHang)
        {
            var khs = from kh in DalCommon.TrangSucDB.KhachHangs
                      where kh.UseName == bizKhachHang.Username && kh.Password == bizKhachHang.Password
                      select new BizKhachHang()
                      {
                          ID = kh.ID,
                          MaKhachHang = kh.MaKhachHang,
                          TenKhachHang = kh.TenKhachHang,
                          DiaChi = kh.DiaChi,
                          Email = kh.Email,
                          Username = kh.UseName,
                          Password = kh.Password,
                          SoDienThoai = kh.SoDienThoai,
                          GhiChu = kh.GhiChu,
                          TaiKhoan = kh.TaiKhoan,
                          Quyen = new BizPhanQuyen()
                          {
                              ID = kh.PhanQuyen.ID,
                              Quyen = kh.PhanQuyen.Quyen
                          }
                      };
            return khs.FirstOrDefault<BizKhachHang>();
        }
        internal static List<BizKhachHang> GetAll()
        {
            var khs = from kh in DalCommon.TrangSucDB.KhachHangs
                      select new BizKhachHang()
                      {
                          ID = kh.ID,
                          MaKhachHang = kh.MaKhachHang,
                          TenKhachHang = kh.TenKhachHang,
                          DiaChi = kh.DiaChi,
                          Email = kh.Email,
                          Username = kh.UseName,
                          Password = kh.Password,
                          SoDienThoai = kh.SoDienThoai,
                          GhiChu = kh.GhiChu,
                          TaiKhoan = kh.TaiKhoan,
                          Quyen = new BizPhanQuyen()
                          {
                              ID = kh.PhanQuyen.ID,
                              Quyen = kh.PhanQuyen.Quyen
                          }
                      };
            return khs.ToList<BizKhachHang>();
        }
        internal static BizKhachHang GetByID(int ID)
        {
            var khs = from kh in DalCommon.TrangSucDB.KhachHangs
                      where kh.ID == ID
                      select new BizKhachHang()
                      {
                          ID = kh.ID,
                          MaKhachHang = kh.MaKhachHang,
                          TenKhachHang = kh.TenKhachHang,
                          DiaChi = kh.DiaChi,
                          Email = kh.Email,
                          Username = kh.UseName,
                          Password = kh.Password,
                          SoDienThoai = kh.SoDienThoai,
                          GhiChu = kh.GhiChu,
                          TaiKhoan = kh.TaiKhoan,
                          Quyen = new BizPhanQuyen()
                          {
                              ID = kh.PhanQuyen.ID,
                              Quyen = kh.PhanQuyen.Quyen
                          }
                      };
            return khs.FirstOrDefault<BizKhachHang>();
        }

        internal static bool Delete(BizKhachHang bizKhachHang)
        {
            try
            {
                TrangSucShopDataContext db = DalCommon.TrangSucDB;
                KhachHang kh = db.KhachHangs.Single(k => k.ID == bizKhachHang.ID);
                db.KhachHangs.DeleteOnSubmit(kh);
                db.SubmitChanges();
                return true;
            }
            catch { return false; }
        }

        internal static bool UpDate(BizKhachHang bizKhachHang)
        {
            try
            {

                TrangSucShopDataContext db = DalCommon.TrangSucDB;
                KhachHang kh = db.KhachHangs.Single(k => k.ID == bizKhachHang.ID);
                kh.MaKhachHang = bizKhachHang.MaKhachHang;
                kh.TenKhachHang = bizKhachHang.TenKhachHang;
                kh.DiaChi = bizKhachHang.DiaChi;
                kh.Email = bizKhachHang.Email;
                kh.SoDienThoai = bizKhachHang.SoDienThoai;
                kh.TaiKhoan = bizKhachHang.TaiKhoan;
                kh.GhiChu = bizKhachHang.GhiChu;
                kh.UseName = bizKhachHang.Username;
                kh.Password = bizKhachHang.Password;
                kh.IDPhanQuyen = bizKhachHang.Quyen.ID;
                db.SubmitChanges();
                return true;
            }
            catch { return false; }
        }

        internal static bool Registry(BizKhachHang _kh)
        {
            KhachHang khach = new KhachHang()
            {
                MaKhachHang = _kh.MaKhachHang,
                TenKhachHang = _kh.TenKhachHang,
                UseName = _kh.Username,
                Password = _kh.Password,
                SoDienThoai = _kh.SoDienThoai,
                Email = _kh.Email,
                DiaChi = _kh.DiaChi,
                IDPhanQuyen = _kh.Quyen.ID,
                TaiKhoan=_kh.TaiKhoan,
            };
            try
            {
                DalCommon.TrangSucDB.KhachHangs.InsertOnSubmit(khach);
                DalCommon.TrangSucDB.SubmitChanges();
                return true;
            }
            catch{return false;}
        }

        internal static BizKhachHang FindByUsername(string _username)
        {
            var khs = from kh in DalCommon.TrangSucDB.KhachHangs
                      where kh.UseName==_username
                      select new BizKhachHang()
                      {
                          ID = kh.ID,
                          MaKhachHang = kh.MaKhachHang,
                          TenKhachHang = kh.TenKhachHang,
                          DiaChi = kh.DiaChi,
                          Email = kh.Email,
                          Username = kh.UseName,
                          Password = kh.Password,
                          SoDienThoai = kh.SoDienThoai,
                          GhiChu = kh.GhiChu,
                          TaiKhoan = kh.TaiKhoan,
                          Quyen = new BizPhanQuyen()
                          {
                              ID = kh.PhanQuyen.ID,
                              Quyen = kh.PhanQuyen.Quyen
                          }
                      };
            return khs.FirstOrDefault<BizKhachHang>();
        }
    }
}
